<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="../js/cookie.js"></script>
    <script src="../js/common.js"></script>
</head>
<body>
    <div>
        <h3> PHP + Mysql 用户登录2  </h3>
        <div class="form">
            <div>
                <label for="account">账号</label>
                <input type="text" id="account" placeholder="请输入用户名/手机号/邮箱">
            </div>
            <div>
                <label for="pass">密码</label>
                <input type="password" id="pass" placeholder="请输入密码">
            </div>
            <div>
                <button id="savebtn" >登录</button>
            </div>
        </div>
    </div>

    <script>
        // 获取元素 
        var accountInp = document.getElementById("account")
        var passInp = document.getElementById("pass")

        var savebtn = document.getElementById("savebtn")

        savebtn.onclick = function(){
            var account = accountInp.value
            var pass = passInp.value 
            if(account&&pass){
                // 链接ajax 访问数据库 
                // ajax -- php 服务器  -- mysql 数据库  --- 返回数据给 php -- 再把数据返给前端 
                var xhr = new XMLHttpRequest()
                xhr.open("POST","../php/login_account.php",true);
                xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
                xhr.send(`account=${account}&pass=${pass}`);    //  后台接收的字段 
                xhr.onreadystatechange = function(){
                    if(xhr.readyState==4 && xhr.status == 200){
                        console.log(xhr.responseText)
                        var result = JSON.parse(xhr.responseText);
                        const {status,detail,user} = result;
                        if(status){
                            // 登录成功
                            // 存储 cookie
                            // 前端 
                            setCookie("loginAccount",user,7);
                            var url = urlParse()
                            console.log(url)
                            // 添加判断 
                            // location.href = './home.html';
                            // if(url.from=="gooddetail"){
                            //     history.back() ; // 直接返回
                            // }else{
                            //     location.href = './home.html';
                            // }
                            if(url.from){
                                if(url.from=="gooddetail"){
                                    history.back() ; // 直接返回
                                }
                            }else{
                                location.href = './home.html';
                            }
                            
                        }else{
                            alert(detail);
                        }
                    }
                }
            }else{
                alert("请先完善登录信息")
            }
        }
    </script>
</body>
</html>